from selenium import webdriver
import unittest, time
import pytesseract
from PIL import Image, ImageEnhance
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class WuliuTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30) #隐性等待时间为30秒
        self.base_url = 'http://120.78.152.214:9499/LezyBeauty/login.html'

    def test_baidu_search(self):
        u"""物流登陆"""
        # 这个可以设置测试用例的 注释
        '''driver = self.driver
        driver.get(self.base_url)
        time.sleep(3)
        driver.find_element_by_id('account').send_keys('ceshi123')
        driver.find_element_by_id('password').send_keys('123456')
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div/div[2]/form/div[4]/input').click()
        time.sleep(3)
        title=driver.title
        #var = driver.find_element_by_id("kw").get_attribute("value")
        self.assertEqual(title, u"鸿泰得国际物流系统v2.0")
        if u"鸿泰得国际物流系统v2.0" == title:
            print('我去取钱')'''

        driver = self.driver
        driver.get(self.base_url)
        driver.maximize_window()  # 最大化窗口
        driver.find_element_by_id("jobNumber").send_keys('13558899722')  # 定位账号输入框
        time.sleep(1)
        driver.find_element_by_id("passWord").send_keys('123456')  # 定位密码输入框
        code1 = driver.find_element_by_id("CheckCode")  # 定位验证码输入框

        yanzhengma = True
        while yanzhengma:
            print('进入到了截图')
            driver.save_screenshot("E://test/testbaogao/img/01.png")  # 截取屏幕内容，保存到本地

            ran = Image.open("E://test/testbaogao/img/01.png")  # 打开截图，获取验证码位置，截取保存验证码
            box = (1580, 536, 1640, 568)  # 获取验证码位置,自动定位不是很明白，就使用了手动定位，代表（左，上，右，下）
            ran.crop(box).save("E://test/testbaogao/img/02.png")  # 把获取的验证码保存
            # 获取验证码图片，读取验证码
            imageCode = Image.open("E://test/testbaogao/img/02.png")  # 打开保存的验证码图片
            # imageCode.load()
            # 图像增强，二值化
            sharp_img = ImageEnhance.Contrast(imageCode).enhance(2.0)
            sharp_img.save("E://test/testbaogao/img/03.png")  # 保存图像增强，二值化之后的验证码图片
            sharp_img.load()  # 对比度增强
            time.sleep(2)
            print(sharp_img)  # 打印图片的信息
            code = pytesseract.image_to_string(sharp_img).strip()  # 读取验证码
            # 5、收到验证码，进行输入验证
            print(code)  # 输出验证码
            # name.send_keys('18502813008')  # 给定位账号的输入框中输入值
            # password.send_keys('1122334455')  # 给定位密码的输入框中输入值
            code1.send_keys(code)  # 给定位验证码的输入框中输入读取到的验证码
            click = driver.find_element_by_id("SubmitBtn").click()  # 点击登录
            if code == '':
                print('没有获取到验证码')
                time.sleep(5)
                driver.find_element_by_xpath("//*[@id='tabBarArea0']/div[4]/table/tbody/tr/td[3]/a").click()  # 点击消费
                time.sleep(1)
                continue
            # 如果出现弹框
            try:
                print('进入到检测弹框')
                # 切换到alert
                WebDriverWait(driver, 5).until(EC.alert_is_present())  # 等待弹出窗口出现
                alert = driver.switch_to.alert
                print(alert.text)
                alert.accept()
                print('关闭了弹框')
            except:
                # 如果没有出现弹框
                print('没有出现弹框，登录成功')
                yanzhengma = False
                time.sleep(3)
                break

    def test_baidu_set(self):
        u"""搜索易景"""
        #这个可以设置测试用例的 注释
        '''driver=self.driver
        driver.get(self.base_url)
        time.sleep(3)
        driver.find_element_by_css_selector('.index_1 > a:nth-child(1) > img:nth-child(1)').click()
        time.sleep(3)
        xiala = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[1]/div/div/i')
        xiala.click()
        time.sleep(3)
        yijing = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[1]/div/dl/dd[4]')
        yijing.click()
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/button[1]').click()
        time.sleep(3)'''
        driver = self.driver
        driver.get(self.base_url)
        time.sleep(3)

        driver.find_element_by_xpath("//*[@id='side-menu']/li[4]/a/span").click()
        print('进入了收银')
        time.sleep(2)

        iframe = driver.find_element_by_xpath('//*[@id="content-main"]/iframe[2]')
        driver.switch_to.frame(iframe)  # 找内联网页

        driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[3]/div[4]/div[2]/p[1]/span[1]").click()
        print('点击一个顾客')
        time.sleep(2)
        # 点击充值
        # driver.find_element_by_id("openTab2").click()
        # time.sleep(2)
        # driver.find_element_by_id("topUpMoney").send_keys('100')
        # time.sleep(2)
        # driver.find_element_by_id("selectGift1").click()      #点击增送金额
        # time.sleep(2)
        # driver.find_element_by_id("home1_giveMoney").send_keys('10')   #输入增送10
        # time.sleep(2)
        # driver.find_element_by_xpath("//*[@id=‘home2’]/div/div[2]/div[1]/div[2]/div/span")  # 点击增送确定
        # time.sleep(2)
        # driver.find_element_by_xpath("//*[@id='home2Closing']/div[2]/span").click()   #点击支付
        # time.sleep(2)
        # driver.find_element_by_id("payCashMoney").send_keys('50')    #现金50
        # time.sleep(2)
        # driver.find_element_by_id("cashType2").click()     #点击支付宝
        # time.sleep(2)
        # driver.find_element_by_xpath("/html/body/div[5]/div[2]/div[3]").click()     #点击收款
        # time.sleep(2)

        # 开始消费
        driver.find_element_by_id("openTab5").click()  # 点击消费
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='contentList']/div[1]/div[2]/div[3]/span/img[2]").click()  # 点击第一个商品+1
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='contentList']/div[2]/div[2]/div[3]/span/img[2]").click()  # 点击第二个商品+1
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="contentList"]/div[2]/div[2]/div[3]/span/img[2]').click()  # 点击第二个商品再+1
        time.sleep(2)
        driver.find_element_by_id('consumeMenu2').click()  # 点击套卡
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="contentList"]/div[2]/div[4]/span[3]/img[2]').click()  # 点击第二个商品+1
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="contentList"]/div[3]/div[4]/span[3]/img[2]').click()  # 点击第三个商品+1
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="contentList"]/div[3]/div[4]/span[3]/img[2]').click()  # 点击第三个商品再+1
        time.sleep(2)
        driver.find_element_by_id('consumeMenu1').click()  # 点击项目
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="72358"]/span[3]/img[2]').click()  # 点击第四个项目+1
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="72438"]/span[3]/img[2]').click()  # 点击第五个项目+1
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="72438"]/span[3]/img[2]').click()  # 点击第五个项目再+1
        time.sleep(2)

        driver.find_element_by_id('selectGift5').click()  # 点击增送商品
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="home2-giftList"]/div').click()  # 点击选择商品
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="71771"]/div[2]/span[3]/img[2]').click()  # 点击第一个商品
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="71772"]/div[2]/span[3]/img[2]').click()  # 点击第二个商品
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="choiceGift"]/div[3]/div[2]').click()  # 点击确定
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="home1"]/div[2]').click()  # 点击结账
        time.sleep(2)
        driver.find_element_by_id('cashType2').click()  # 点击支付宝
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="layui-layer2"]/div[2]/div[3]').click()  # 点击确定
        time.sleep(2)
        print('消费完成了')

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()
